Summary of Methods of Data Rounding in PHP [4 Methods]
- 2021-12-04 18:13:59
- OfStack
In this paper, an example is given to summarize the method of PHP to realize data 4 rounding and 5 inputting. Share it for your reference, as follows:
In the development of PHP, sometimes we encounter the operation of 4 rounding and 5 inputting data. This paper shares four methods to realize 4 rounding and 5 inputting data with PHP.
php realizes four methods of data 4 rounding and 5 entering, respectively through
number_format()
Function,
round()
Function sum
sprintf()
Formatting and
intval()
The method of function output realizes 4 rounding and 5 inputting. Strictly speaking, the last one is not strictly 4 rounded by 5. The last one only removes the decimal part of the data and keeps only the integer part. Details will be discussed below.
1. number_format () method realizes 4 rounding and 5 inputting
number_format()
Function to format numbers by grouping thousands of bits.
<?php
// Definition 1 A float Variable of type
$number = 1234.5678;
//English Notation (defult)
$number_format_english = number_format($number);
//1,235
$number_format_english = number_format($number, 2, '.', '');
//1234.57
//French Notation
$number_format_francais = number_format($number, 2, ',', '');
//1234,57
$number_format_francais = number_format($number, 3, ',', '');
//1234,568
echo $number_format_english;
//1234.57
echo $number_format_francais;
//1234,568
2. round () method realizes 4 rounding and 5 inputting
round()
Function to round up a floating-point number by 4 and enter it by 5.
<?php
// Definition 1 A float Variable of type
$number = 1234.5678;
// Do not retain decimals
echo round($number);
//1235
// Keep two young trees
echo round($number,2);
//1234.57
echo "<br>";
$number = 12345678;
// Proceed in the thousandth 4 Shed 5 Into
echo round($number,-4);
//12350000
3. sprintf () formatted input realizes 4 rounding and 5 inputs
String formatting command, the main function is to write formatted data into a string.
sprintf
It's a variable parameter function.
<?php
// Definition 1 Positive integers
$n = 43951789;
// Definition 1 Negative integers
$u = -43951789;
// ASCII 65 is 'A'
$c = 65;
printf("%%b = '%b'\n", $n);
//%b = '10100111101010011010101101'
printf("%%c = '%c'\n", $c);
//%c = 'A'
printf("%%d = '%d'\n", $n);
//%d = '43951789'
printf("%%e = '%e'\n", $n);
//%e = '4.395179e+7'
printf("%%u = '%u'\n", $n);
//%u = '43951789'
printf("%%u = '%u'\n", $u);
//%u = '4251015507'
printf("%%f = '%f'\n", $n);
//%f = '43951789.000000'
printf("%%o = '%o'\n", $n);
//%o = '247523255'
printf("%%s = '%s'\n", $n);
//%s = '43951789'
printf("%%x = '%x'\n", $n);
//%x = '29ea6ad'
printf("%%X = '%X'\n", $n);
//%X = '29EA6AD'
printf("%%+d = '%+d'\n", $n);
//%+d = '+43951789'
printf("%%+d = '%+d'\n", $u);
//%+d = '-43951789'
4. The intval () function realizes integer output
This method is not a strict 4-round-5 implementation, which forcibly clears the decimal part of the data to achieve the effect of only outputting the integer part.
<?php
// Definition 1 Floating point number
$number = 1234.5678;
$number_int = intval($number);
echo $number_int;
//1234
PS: Here are some computing tools recommended for your reference in the next step:
On-line 1 yuan function (equation) solution calculation tool;
http://tools.ofstack.com/jisuanqi/equ_jisuanqi
Scientific calculator online _ advanced calculator online calculation:
http://tools.ofstack.com/jisuanqi/jsqkexue
Online Calculator _ Standard Calculator:
http://tools.ofstack.com/jisuanqi/jsq
For more readers interested in PHP related contents, please check the special topics of this site: "Summary of PHP Mathematical Operation Skills", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Encyclopedia of PHP Array (Array) Operation Skills", "Tutorial on PHP Data Structure and Algorithm", "Summary of php Programming Algorithm" and "Summary of php Regular Expression Usage"
I hope this article is helpful to everyone's PHP programming.